package cn.jolyoulu.adminweb.service.impl;


import cn.jolyoulu.adminweb.entity.bo.form.SysDictDataFormBO;
import cn.jolyoulu.adminweb.entity.bo.form.SysDictFormBO;
import cn.jolyoulu.adminweb.entity.bo.query.SysDictQueryBO;
import cn.jolyoulu.adminweb.entity.vo.SysDictDataVO;
import cn.jolyoulu.adminweb.entity.vo.SysDictTypeVO;
import cn.jolyoulu.cmnservice.enums.CmnExpType;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.jolyoulu.adminweb.service.ApiSysDictService;
import cn.jolyoulu.cmnservice.constant.ServiceRedisConstant;
import cn.jolyoulu.cmnservice.entity.po.SysDictData;
import cn.jolyoulu.cmnservice.entity.po.SysDictType;
import cn.jolyoulu.cmnservice.serivce.SysDictDataService;
import cn.jolyoulu.cmnservice.serivce.SysDictTypeService;
import cn.jolyoulu.common.mybatis.plus.plugin.page.JlPage;
import cn.jolyoulu.common.redis.utils.RedisUtils;
import cn.jolyoulu.common.web.enums.GlobalExpType;
import cn.jolyoulu.common.web.excption.GlobalException;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author LuZhouJin
 * @Date 2023/3/17
 */
@Slf4j
@Service
public class ApiSysDictServiceImpl implements ApiSysDictService {

    @Autowired
    private SysDictTypeService sysDictTypeService;

    @Autowired
    private SysDictDataService sysDictDataService;

    @Autowired
    private RedisUtils redisUtils;

    @Override
    public JlPage<SysDictTypeVO> pageList(SysDictQueryBO bo) {
        JlPage<SysDictTypeVO> res = JlPage.execute(bo.getPage(), bo.getPageSize(),
                () -> {
                    LambdaQueryWrapper<SysDictType> ew = new LambdaQueryWrapper<>();
                    if (StringUtils.isNotBlank(bo.getDictName())){
                        ew.like(SysDictType::getDictName,bo.getDictName());
                    }
                    if (StringUtils.isNotBlank(bo.getDictType())){
                        ew.like(SysDictType::getDictType,bo.getDictType());
                    }
                    List<SysDictType> list = sysDictTypeService.list(ew);
                    return list.stream().map(SysDictTypeVO::new).collect(Collectors.toList());
                });
        return res;
    }

    @Override
    public SysDictTypeVO getById(String id) {
        SysDictType sysDictType = sysDictTypeService.getById(id);
        return new SysDictTypeVO(sysDictType);
    }

    @Override
    @Transactional
    public void add(SysDictFormBO sysDictFormBO) {
        //基础唯一性
        SysDictType check = sysDictTypeService.getByDictType(sysDictFormBO.getDictType());
        if (Objects.nonNull(check)){
            throw new GlobalException(CmnExpType.SYSDICTT_TYPE_ERROR);
        }
        SysDictType dictType = new SysDictType();
        BeanUtils.copyProperties(sysDictFormBO,dictType);
        dictType.fillUpdate().fillCreat();
        sysDictTypeService.save(dictType);
    }

    @Override
    @Transactional
    public void update(SysDictFormBO sysDictFormBO) {
        //不能修改的属性
        sysDictFormBO.setDictType(null);
        SysDictType dictType = new SysDictType();
        BeanUtils.copyProperties(sysDictFormBO,dictType);
        dictType.fillUpdate();
        sysDictTypeService.updateById(dictType);
    }

    @Override
    @Transactional
    public void delByIds(List<String> ids) {
        for (String id : ids) {
            sysDictTypeService.removeById(id);
            sysDictDataService.removeByDictTypeId(id);
        }
        if (!ids.isEmpty()){
            //清理缓存
            SysDictType dictType = sysDictTypeService.getById(ids.get(0));
            if (Objects.nonNull(dictType)){
                SysDictType dict = sysDictTypeService.getById(dictType.getDictType());
                String key = ServiceRedisConstant.getDictListDicttype(dict.getDictType());
                if (redisUtils.exists(key)){
                    redisUtils.del(key);
                }
            }
        }
    }

    @Override
    public List<SysDictDataVO> dataList(String dictTypeId) {
        List<SysDictData> list = sysDictDataService.selectByDictTypeId(dictTypeId);
        return list.stream().map(SysDictDataVO::new).collect(Collectors.toList());
    }

    @Override
    public SysDictDataVO getDataById(String id) {
        SysDictData res = sysDictDataService.getById(id);
        return new SysDictDataVO(res);
    }

    @Override
    @Transactional
    public void addData(SysDictDataFormBO sysDictDataFormBO) {
        //基础唯一性
        SysDictData check = sysDictDataService
                .getByDictTypeIdAndDictValue(sysDictDataFormBO.getDictTypeId(),sysDictDataFormBO.getDictValue());
        if (Objects.nonNull(check)){
            throw new GlobalException(CmnExpType.SYSDICTT_DATA_VALUE_ERROR);
        }
        SysDictData dictData = new SysDictData();
        BeanUtils.copyProperties(sysDictDataFormBO,dictData);
        dictData.fillUpdate().fillCreat();
        sysDictDataService.save(dictData);
        //清理缓存
        if (StringUtils.isNotBlank(dictData.getDictTypeId())){
            SysDictType dict = sysDictTypeService.getById(dictData.getDictTypeId());
            if (Objects.nonNull(dict)){
                String key = ServiceRedisConstant.getDictListDicttype(dict.getDictType());
                if (redisUtils.exists(key)){
                    redisUtils.del(key);
                }
            }
        }
    }

    @Override
    @Transactional
    public void editData(SysDictDataFormBO sysDictDataFormBO) {
        //基础唯一性
        SysDictData check = sysDictDataService
                .getByDictTypeIdAndDictValue(sysDictDataFormBO.getDictTypeId(),sysDictDataFormBO.getDictValue());
        if (Objects.nonNull(check)){
            throw new GlobalException(CmnExpType.SYSDICTT_DATA_VALUE_ERROR);
        }
        SysDictData dictData = new SysDictData();
        BeanUtils.copyProperties(sysDictDataFormBO,dictData);
        dictData.fillUpdate();
        sysDictDataService.updateById(dictData);
        //清理缓存
        if (StringUtils.isNotBlank(dictData.getDictTypeId())){
            SysDictType dict = sysDictTypeService.getById(dictData.getDictTypeId());
            String key = ServiceRedisConstant.getDictListDicttype(dict.getDictType());
            if (redisUtils.exists(key)){
                redisUtils.del(key);
            }
        }
    }

    @Override
    @Transactional
    public void delData(List<String> ids) {
        for (String id : ids) {
            sysDictDataService.removeById(id);
        }
        if (!ids.isEmpty()){
            //清理缓存
            SysDictData dictData = sysDictDataService.getById(ids.get(0));
            if (Objects.nonNull(dictData)){
                SysDictType dict = sysDictTypeService.getById(dictData.getDictTypeId());
                String key = ServiceRedisConstant.getDictListDicttype(dict.getDictType());
                if (redisUtils.exists(key)){
                    redisUtils.del(key);
                }
            }
        }
    }
}
